//
//  AdvanceDestoryLeaveViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/11/2.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//  提前销假

#import "AdvanceDestoryLeaveViewController.h"
#import "AdvanceDestoryLeaveView.h"
#import "ChosePayView.h"

@interface AdvanceDestoryLeaveViewController ()<ChoseBtnDelegate>

@property(nonatomic,weak)AdvanceDestoryLeaveView *advanceDestoryView;

@property(nonatomic,strong)NSMutableDictionary *dataDict;

@end

@implementation AdvanceDestoryLeaveViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"提前销假";
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnClick)];
    
    [rightButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CSBlackColor,NSFontAttributeName:MidFont} forState:UIControlStateNormal];
    
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
    [self createSubView];
}

-(void)createSubView{
    
    AdvanceDestoryLeaveView *advanceDestoryView = [[AdvanceDestoryLeaveView alloc] init];
    
    advanceDestoryView.choseBtnDelegate = self;
    
    NSString *startTime = [self.leaveDict objectForKey:@"BeginDate"];
    
    advanceDestoryView.originalLabel.text = startTime;
    
    NSDateComponents *component = [NSString getDateDifference:startTime];
    
    advanceDestoryView.dayCountLabel.text = [NSString stringWithFormat:@"%ld",labs(component.day)];
    
    //销毁后有效期
    
    NSDateFormatter *df = [[NSDateFormatter alloc] init];//格式化
    [df setDateFormat:@"yyyy-MM-dd"];
    NSString * endTime = [df stringFromDate:[NSDate date]];
    
    advanceDestoryView.destoryTimeLabel.text = [NSString stringWithFormat:@"%@至%@",[self.leaveDict objectForKey:@"BeginDate"],endTime];//
    
    //原有效期
    advanceDestoryView.validTimeLabel.text = [NSString stringWithFormat:@"原有效期：%@ 至 %@",[self.leaveDict objectForKey:@"BeginDate"],[self.leaveDict objectForKey:@"EndDate"]];
    
    [self.view addSubview:advanceDestoryView];
    
    self.advanceDestoryView = advanceDestoryView;
    
    [advanceDestoryView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(self.view);
    }];
}

#pragma mark 支付方式选择
-(void)chosePayBtnClick{
    
    [self.view endEditing:YES];
    
    __weak typeof(self)weakSelf = self;
    
    ChosePayView *chosePayView = [[ChosePayView alloc] init];
    
    [chosePayView showWithAnimation];
    
    chosePayView.commitBlock = ^(NSString *str,NSString *strId) {
        
        weakSelf.advanceDestoryView.payStyleView.choseLabel.text = str;
        
        weakSelf.advanceDestoryView.payStyleView.choseLabel.textColor = TFColor;
        
        [weakSelf.dataDict setObject:strId forKey:@"ReturnPayType"];
    };
}

#pragma mark 右侧按钮点击

-(void)rightBtnClick{
    
    //支付方式
    [self.dataDict setObject:self.advanceDestoryView.refundSwitchView.titleSwitch.isOn?@"1":@"0" forKey:@"IsReturnFee"];        
    
    if (self.advanceDestoryView.moneyInputView) {
        
        //金额
        [self.dataDict setObject:self.advanceDestoryView.moneyInputView.titleTF.text forKey:@"ReturnFee"];
    }
    
    [self.dataDict setObject:[self.leaveDict objectForKey:@"ID"] forKey:@"ID"];
    
    [MBProgressHUD showMessage:@"请求中..." toView:self.view];
    
    [HTTPTool POSTRaw:Post_Leave_LeaveCancel parameters:self.dataDict
     
              success:^(id responseObject){
                  
                  [MBProgressHUD hideHUDForView:self.view];
                  
                  if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                      
                      [MBProgressHUD showText:@"销假成功成功"];
                      
                      [NotificationCenter postNotificationName:@"ReloadLeave" object:nil];
                      
                      [self.navigationController popViewControllerAnimated:YES];
                  }
                  
              }
     
              failure:^(NSError *error){
                  
                  [MBProgressHUD hideHUDForView:self.view];
              }];
}


#pragma mark 数据返回
-(NSMutableDictionary *)dataDict{
    
    if (!_dataDict) {
        
        _dataDict = [[NSMutableDictionary alloc] init];
    }
    
    return _dataDict;
}
@end
